﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using billtracker.Models;

namespace billtracker.Controllers
{
   
    public class BillsController : Controller
    {
        private billDBContext db = new billDBContext();


        public ActionResult _Create()
        {
            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name");
            return PartialView();
        }
       
        //
        // GET: /Bills/
        public ViewResult Index()
        {
            var bill = db.Bill.Include(b => b.Frequency);
            return View(bill.ToList());
        }
        public ActionResult _Index()
        {
            var bill = db.Bill.Include(b => b.Frequency);
            return PartialView(bill.ToList());
        }
        //
        // GET: /Bills/Details/5
        public ViewResult Details(int id)
        {
            bill bill = db.Bill.Find(id);
            return View(bill);
        }

        //
        // GET: /Bills/Create

        public ActionResult Create()
        {
            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name");
            return View();
        } 

        //
        // POST: /Bills/Create

        [HttpPost]
        public ActionResult Create(bill bill)
        {
            if (ModelState.IsValid)
            {
                db.Bill.Add(bill);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name", bill.frequencyId);
            return View(bill);
        }
        
        //
        // GET: /Bills/Edit/5
 
        public ActionResult Edit(int id)
        {
            bill bill = db.Bill.Find(id);
            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name", bill.frequencyId);
            return View(bill);
        }

        //
        // POST: /Bills/Edit/5

        [HttpPost]
        public ActionResult Edit(bill bill)
        {
            if (ModelState.IsValid)
            {
                db.Entry(bill).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name", bill.frequencyId);
            return View(bill);
        }
        //
        // GET: /Bills/Edit/5

        public ActionResult _Edit(int id)
        {
            bill bill = db.Bill.Find(id);
            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name", bill.frequencyId);
            return PartialView(bill);
        }
        //
        // GET: /Bills/Delete/5

        public ActionResult _Delete(int id)
        {
            bill bill = db.Bill.Find(id);
            //ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name", bill.frequencyId);
            return PartialView(bill);
        }
        //
        // POST: /Bills/Edit/5

        [HttpPost]
        public ActionResult _Edit(bill bill)
        {
            if (ModelState.IsValid)
            {
                db.Entry(bill).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.frequencyId = new SelectList(db.Frequency, "frequencyId", "name", bill.frequencyId);
            return PartialView(bill);
        }

       
        //
        // GET: /Bills/Delete/5 
        public ActionResult Delete(int id)
        {
            bill bill = db.Bill.Find(id);
            return View(bill);
        }

        //
        // POST: /Bills/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            bill bill = db.Bill.Find(id);
            db.Bill.Remove(bill);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult SearchIndex(string searchString)
        {
            var bill = from b in db.Bill
                       select b;
            if(!String.IsNullOrEmpty(searchString)){
                bill = bill.Where(s => s.name.Contains(searchString));
            }
            return View(bill);
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        [HttpPost]
        public JsonResult UpdateBill(bill bill)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(false);
                }
                db.Entry(bill).State = EntityState.Modified;
                db.SaveChanges();
                return Json(true);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
        [HttpPost]
        public JsonResult DeleteBill(bill bill)
        {
            try
            {
               
                db.Entry(bill).State = EntityState.Deleted;
                db.SaveChanges();
                return Json(true);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
        [HttpPost]
        public JsonResult CreateBill(bill bill)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(false);
                }

                db.Bill.Add(bill);
                db.SaveChanges();
                return Json(true);
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
       
        //
        // GET: /Bills/
        public ViewResult grid()
        {
            var bill = db.Bill.Include(b => b.Frequency);
            return View(bill.ToList());
        }
    }
}